# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import scrapy
import json
import csv


class TransactionPolicyPipeline:

    def open_spider(self, spider):
        self.file = open('上海交易规则.csv', 'w', newline='', encoding='utf-8')
        self.writer = csv.writer(self.file)
        self.writer.writerow(['title', 'fabu_time', 'url', 'source', 'content'])  # 写入标题行，根据需要修改字段名称

    def close_spider(self, spider):
        self.file.close()

    def process_item(self, item, spider):
        self.writer.writerow([item['title'], item["fabu_time"], item["url"], item['source'], item['content']])  # 写入数据行，根据需要修改字段名称
        return item
